﻿using UnityEngine;
using UnityEngine.UI;
#if UNITY_EDITOR
using UnityEditor;
#endif

namespace Jinndev {

    /// <summary>
    /// 继承ContentSizeFitter，相比增加了最小大小限制
    /// </summary>
    public class CustomContentSizeFitter : ContentSizeFitter {

        public float minHorizontal;
        public float maxHorizontal;
        public float minVertical;
        public float maxVertical;

        [System.NonSerialized]
        private RectTransform m_Rect;

        private RectTransform rectTransform {
            get {
                if (m_Rect == null) {
                    m_Rect = GetComponent<RectTransform>();
                }
                return m_Rect;
            }
        }

        public override void SetLayoutHorizontal() {
            base.SetLayoutHorizontal();

            if (horizontalFit == FitMode.PreferredSize) {
                float x = rectTransform.rect.width;
                x = Mathf.Clamp(x, minHorizontal, maxHorizontal > 0 ? maxHorizontal : x);
                rectTransform.SetSizeWithCurrentAnchors(RectTransform.Axis.Horizontal, x);
            }
        }

        public override void SetLayoutVertical() {
            base.SetLayoutVertical();

            if (verticalFit == FitMode.PreferredSize) {
                float y = rectTransform.rect.height;
                y = Mathf.Clamp(y, minVertical, maxVertical > 0 ? maxVertical : y);
                rectTransform.SetSizeWithCurrentAnchors(RectTransform.Axis.Vertical, y);
            }
        }

    }

#if UNITY_EDITOR
    [CustomEditor(typeof(CustomContentSizeFitter))]
    [CanEditMultipleObjects]
    public class CustomContentSizeFitterRditor : Editor {

        private SerializedProperty m_HorizontalFit;
        private SerializedProperty m_VerticalFit;
        private SerializedProperty minHorizontal;
        private SerializedProperty maxHorizontal;
        private SerializedProperty minVertical;
        private SerializedProperty maxVertical;

        private void OnEnable() {
            m_HorizontalFit = serializedObject.FindProperty("m_HorizontalFit");
            m_VerticalFit = serializedObject.FindProperty("m_VerticalFit");
            minHorizontal = serializedObject.FindProperty("minHorizontal");
            maxHorizontal = serializedObject.FindProperty("maxHorizontal");
            minVertical = serializedObject.FindProperty("minVertical");
            maxVertical = serializedObject.FindProperty("maxVertical");
        }

        public override void OnInspectorGUI() {
            serializedObject.Update();
            EditorGUILayout.PropertyField(m_HorizontalFit);
            if (m_HorizontalFit.enumValueIndex == (int)ContentSizeFitter.FitMode.PreferredSize) {
                EditorGUI.indentLevel = 1;
                EditorGUILayout.PropertyField(minHorizontal);
                EditorGUILayout.PropertyField(maxHorizontal);
                EditorGUI.indentLevel = 0;
            }
            EditorGUILayout.PropertyField(m_VerticalFit);
            if (m_VerticalFit.enumValueIndex == (int)ContentSizeFitter.FitMode.PreferredSize) {
                EditorGUI.indentLevel = 1;
                EditorGUILayout.PropertyField(minVertical);
                EditorGUILayout.PropertyField(maxVertical);
                EditorGUI.indentLevel = 0;
            }
            serializedObject.ApplyModifiedProperties();
        }

    }
#endif


}